﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WCS.Controls;
using WCS.Utils;

namespace WCS.Entity
{
    [SugarTable("WCS_WarehouseInfo")]
    public class WarehouseInfo
    {
        /// <summary>
        /// 仓库ID
        /// </summary>
        public string ID { get; set; }

        /// <summary>
        /// 托盘序号
        /// </summary>
        public int index { get; set; }

        /// <summary>
        /// 托盘号
        /// </summary>
        public string barCode { get; set; }

        /// <summary>
        /// 托盘1的数据内容
        /// </summary>
        public string BarCodeData { get; set; }

        /// <summary>
        /// 库存状态
        /// </summary>
        public int state { get; set; }

        /// <summary>
        /// 库存状态
        /// </summary>
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 仓库号
        /// </summary>
        public string WarehouseCode { get; set; }

        /// <summary>
        /// 物料库存数量
        /// </summary>
        public string num { get; set; }
        
        /// <summary>
        /// 可用物料数量
        /// </summary>
        public string allowOutNum { get; set; }

        /// <summary>
        /// 物料类型
        /// </summary>
        public string materialtype { get; set; }

        /// <summary>
        /// 入库时间
        /// </summary>
        [SugarColumn(IsIgnore = true)]
        public int dataIff { get; set; }

        /// <summary>
        /// 数据库连接对象
        /// </summary>
        static SqlSugarDapper _SqlSugarDapper = SqlSugarDapper.GetDbContext();

        /// <summary>
        /// 根据ID获取第一个的库存信息
        /// </summary>
        /// <param name="material"></param>
        /// <returns></returns>
        public static WarehouseInfo GetWarehouseInfoById(string Id)
        {
            return _SqlSugarDapper.DB.SqlQueryable<WarehouseInfo>("SELECT *,DATEDIFF(hh, CreateTime,GETDATE()) as dataIff from WCS_WarehouseInfo").Where(w => w.ID == Id).OrderBy(o => o.index).First();
        }

        /// <summary>
        /// 根据ID获取第一个的库存信息
        /// </summary>
        /// <param name="material"></param>
        /// <returns></returns>
        public static WarehouseInfo GetWarehouseInfoByBarCode(string barCode, string id)
        {
            return _SqlSugarDapper.DB.Queryable<WarehouseInfo>().Where(w => w.barCode == barCode && w.state == (int)StationState.出库占用 && w.ID == id).OrderBy(o => o.index).First();
        }

        /// <summary>
        /// 添加库存信息
        /// </summary>
        /// <param name="material"></param>
        /// <returns></returns>
        public static void AddWarehouseInfo(WarehouseInfo warehouseInfo)
        {
            _SqlSugarDapper.DB.Insertable(warehouseInfo).ExecuteCommand();
        }

        /// <summary>
        /// 根据托盘号修改库存状态
        /// </summary>
        /// <param name="material"></param>
        /// <returns></returns>
        public static void ChangeWarehouseInfo(WarehouseInfo warehouseInfo, int state)
        {
            var i = _SqlSugarDapper.DB.Updateable<WarehouseInfo>()
                .SetColumns(s => s.state == state)
                .Where(w => w.ID == warehouseInfo.ID && w.index == warehouseInfo.index && w.barCode == warehouseInfo.barCode).ExecuteCommand();
        }

        /// <summary>
        /// 根据巷道和层数获取物料的排序号
        /// </summary>
        /// <param name="material"></param>
        /// <returns></returns>
        public static int GetWarehouseInfoIndex(string ID)
        {
            var WarehouseInfos = _SqlSugarDapper.DB.Queryable<WarehouseInfo>().Where(w => w.ID == ID  && w.index != 999999999).OrderByDescending(o => o.index).ToList();

            if (WarehouseInfos.Count == 0)
            {
                return 1;
            }

            if (WarehouseInfos.First().index > 100000)
            {
                //更新托盘index
                for (int i = WarehouseInfos.Count - 1; i >= 0; i--)
                {
                    var warehouseInfo = WarehouseInfos[i];
                    _SqlSugarDapper.DB.Updateable<WarehouseInfo>()
                        .SetColumns(s => s.index == WarehouseInfos.Count - i)
                        .Where(w => w.index == warehouseInfo.index && w.barCode == warehouseInfo.barCode && w.ID == warehouseInfo.ID).ExecuteCommand();
                }

                WarehouseInfos = _SqlSugarDapper.DB.Queryable<WarehouseInfo>().Where(w => w.ID == ID && w.index != 999999999).OrderByDescending(o => o.index).ToList();
            }
            return WarehouseInfos.First().index + 1;
        }

        /// <summary>
        /// 根据托盘号删除信息
        /// </summary>
        /// <param name="material"></param>
        /// <returns></returns>
        public static int DeleteWarehouseInfo(WarehouseInfo info)
        {
            //根据物料号查询这个物料在那个仓库的第几层
            //_SqlSugarDapper.DB.Deleteable<WarehouseInfo>().Where(w => w.barCode == barCode ).ExecuteCommand();
            return _SqlSugarDapper.DB.Deleteable<WarehouseInfo>()
                .Where(w => w.ID == info.ID && w.index == info.index && w.barCode == info.barCode && w.state == (int)StationState.出库占用).ExecuteCommand();
            //var i = _SqlSugarDapper.DB.Ado.ExecuteCommand($"DELETE WCS_WarehouseInfo WHERE barCode = '{barCode}' and ID = '{code}' and state = '75'");
        }
    }
}
